#pragma once

#include "FileReader.h"

class FileReaderPool : public FileReader
{
public:
	FileReaderPool();
	virtual ~FileReaderPool();

	bool Init(IN stringc& strFile, IN u32 useMemory, IN u32 reservedSpace, IN u32 readFlag);
	void Uninit();

	u64 GetFileSize();

	bool GetData(IN u64 offset, OUT u8* data, IN u32 size);
	bool Getu64(IN u64 offset, OUT u64& v);
	bool Getu32(IN u64 offset, OUT u32& v);
	bool Getu16(IN u64 offset, OUT u16& v);
	bool Getu8(IN u64 offset, OUT u8& v);

	void SetReservedSpace(IN u32 reservedSpace);

protected:
	void Clear();
	void CheckParameter();
	bool RePool(IN u64 offset, IN u32 size);
	bool RePoolNoOverlapped(IN u64 offset, IN u32 size);
	bool RePoolOverlapped(IN u64 offset, IN u32 size);
	void ChangePool();
	bool ReadFileToPool1(IN u64 offset, IN u32 size);
	bool ReadFileToPool2();
	bool ReadFileImmediately(IN u64 offset, OUT u8* data, INOUT u32& size);
	bool CheckSize(IN u64 offset1, IN u64 size1, IN u64 offset2, IN u64 size2);		// check if #1 contains #2
	bool CalcFileOffsetAndFillSize(IN u64 offset, IN u32 size, OUT u64& fileOffset, OUT u32& fillSize);

private:
	// parameter
	u32 m_useMemory;
	u32 m_reservedSpace;
	u32 m_readFlag;

	// 
	HANDLE m_hFile;
	u64 m_fileSize;
	u32 m_poolSize;
	u32 m_innerFlag;

	u8* m_pool1;
	u64 m_fileOffset1;
	u32 m_fillSize1;
	
	u8* m_pool2;
	u64 m_fileOffset2;
	u32 m_fillSize2;

	OVERLAPPED m_tempOverLapped2;
	u64 m_tempFileOffset2;
	u32 m_tempFillSize2;
	
};